#code republican, democrat, independent
data_panel$pid <- data_panel$pid3
data_panel$pid[data_panel$pid>3] <- 3 #store other as independent
data_panel$pid <- recode(data_panel$pid, `1` = "Democrat", `2`= "Republican", `3` = "Independent")
data_panel$pid <- as.factor(data_panel$pid)
data_panel$pid <-relevel(data_panel$pid, "Democrat") #code relative to dems

data_cross$pid <- data_cross$pid3
data_cross$pid[data_cross$pid>3] <- 3 #store other as independent
data_cross$pid <- recode(data_cross$pid, `1` = "Democrat", `2`= "Republican", `3` = "Independent")
data_cross$pid <- as.factor(data_cross$pid)
data_cross$pid <-relevel(data_cross$pid, "Democrat") #code relative to dems

#store leak and decision cut variables
data_panel <- mutate(data_panel,
                     leak_cut = case_when(
                       wave > 0 ~ 1, #post-leak
                       TRUE ~ 0
                     ))

data_panel <- mutate(data_panel,
                     decision_cut = case_when(
                       wave > 1 ~ 1, #post-decision
                       TRUE ~ 0
                     ))

data_cross <- mutate(data_cross,
                     leak_cut = case_when(
                       wave > 0 ~ 1, #post-leak
                       TRUE ~ 0
                     ))

data_cross <- mutate(data_cross,
                     decision_cut = case_when(
                       wave > 1 ~ 1, #post-decision
                       TRUE ~ 0
                     ))

##### calculate days since variable #######
#leak on 2022-05-02 , decision on 2022-06-24
data_panel$dayssince_leak = as.Date(data_panel$date) - as.Date('2022-05-02')
data_panel$dayssince_decision = as.Date(data_panel$date) - as.Date('2022-06-24')

data_cross$dayssince_leak = as.Date(data_cross$date) - as.Date('2022-05-02')
data_cross$dayssince_decision = as.Date(data_cross$date) - as.Date('2022-06-24')


####### store dv's  #########
dvs <- c('personal_dobbs', 'personal_roe', 'courtlegit_composite',
         'approve_court', 'courtsize', 'courtterms',
         'courtideology')

dvs2 <- c('norm', 'moral_norm', 'opinion', 'moral_opinion', 'fetal_scale')

dv_last <- c('lagged_personal_dobbs', 'lagged_personal_roe',
             'lagged_courtlegit_composite',
             'lagged_approve_court',
             'lagged_courtsize', 'lagged_courtterms',
             'lagged_courtideology')

extras <- c('court_represent', 'personal_affirmative', 'oersonal_rifle',
            'pride', 'political_interest', 'women_feelings',
            'dem_feelings', 'rep_feelings', 'parental_leave')

mods <- c('pid', 'religion_prolife_recoded', 'importance_recoded',
          'courtlegit_recoded', 'courtknow_composite', 'news_binary',
          'info_source_sm', 'info_source_cable', 'info_source_local', 'info_source_friend',
          'info_source_online', 'info_source_newspaper', 'info_source_email',
          'info_source_church')

#correlation matrix
table(data_panel$wave)
w1 <- data_panel %>% filter(wave == 0) %>% 
  select(c(courtlegit_1:courtlegit_5,courtlegit_composite, approve_court)) %>% drop_na()

# w1$courtlegit_1 = w1$c1
# w1$courtlegit_2 = w1$c2 
# w1$courtlegit_3 = w1$c3 
# w1$courtlegit_4 = w1$c4 
# w1$courtlegit_5 = w1$c5
# w1$courtlegit_composite = w1$cc 
# w1$approve_court = w1$c_a

datamatrix <- cor(w1)
corrplot(datamatrix, method="number")

#####################################################################
###################### pre-registered analyses ######################
#####################################################################

####### panel main analyses #########
for(i in 1:length(dvs)){
  
  simple_model <- 
    lm_robust(data_panel[,dvs[i]] ~ leak_cut + decision_cut,
              se_type = "HC1", 
              fixed_effects = ~ universal_ID,
              weights= weight_ps,
              data = data_panel)
  
  #lagged DV (decision only)
  lag_model <- 
    lm_robust(data_panel[,dvs[i]] ~ decision_cut + data_panel[,dv_last[i]],
              fixed_effects = ~ universal_ID,
              se_type = "HC1",
              data = data_panel)
  
  # time model (leak only)
  time_model_leak <- 
    lm_robust(data_panel[,dvs[i]] ~ leak_cut*dayssince_leak,
              se_type = "HC1",
              fixed_effects = ~ universal_ID,
              data = data_panel)
  
  time_model_decision <- 
    lm_robust(data_panel[,dvs[i]] ~ decision_cut*dayssince_decision,
              se_type = "HC1",
              fixed_effects = ~ universal_ID,
              data = data_panel)
  
  texreg(list(simple_model, lag_model, time_model_leak, time_model_decision), digits = 2, include.ci = TRUE, 
         file = glue("Tables/panel_{dvs[i]}.tex"),
         stars = c(0.001, 0.01, 0.05),
         custom.model.names = c("1", "2", "3", "4"), 
         custom.coef.names= c("Leak", "Decision", "Lagged DV", "Days Since Leak", "Leak*Days Since", "Days Since Decision", "Decision*Days Since"), 
         caption= dvs[i],
         caption.above = TRUE,
         custom.note = "*$p<.05$.")
}


##### two-wave dv's  #######

for(i in 1:length(dvs2)){
  #fe by ID
  simple_model <- 
    lm_robust(data_panel[,dvs2[i]] ~ decision_cut,
              se_type = "HC1", 
              fixed_effects = ~ universal_ID,
              weights= weight_ps,
              data = data_panel)
  
  # time model 
  time_model_decision <- 
    lm_robust(data_panel[,dvs2[i]] ~ decision_cut*dayssince_decision,
              se_type = "HC1",
              fixed_effects = ~ universal_ID,
              data = data_panel)
  
  texreg(list(simple_model, time_model_decision), digits = 2, include.ci = TRUE, 
         file = glue("Tables/panel_{dvs2[i]}.tex"),
         stars = c(0.001, 0.01, 0.05),
         custom.model.names = c("1", "2"), 
         custom.coef.names= c("Decision", "Days Since Decision", "Decision*Days Since"), 
         caption= dvs2[i],
         caption.above = TRUE,
         custom.note = "*$p<.05$.")
}



# cross-sectional analyses ------------------------------------------------

####### panel main analyses #########
for(i in 1:length(dvs)){
  
  simple_model <- 
    lm_robust(data_cross[,dvs[i]] ~ leak_cut + decision_cut,
              se_type = "HC1", 
              weights= weight,
              data = data_cross)
  
  # time model (leak only)
  time_model_leak <- 
    lm_robust(data_cross[,dvs[i]] ~ leak_cut*dayssince_leak,
              se_type = "HC1",
              data = data_cross)
  
  time_model_decision <- 
    lm_robust(data_cross[,dvs[i]] ~ decision_cut*dayssince_decision,
              se_type = "HC1",
              data = data_cross)
  
  texreg(list(simple_model, time_model_leak, time_model_decision), digits = 2, include.ci = TRUE, 
         file = glue("Tables/cross_{dvs[i]}.tex"),
         stars = c(0.001, 0.01, 0.05),
         custom.model.names = c("1", "2", "3"), 
         custom.coef.names= c("(Intercept)", 'Leak', "Decision", "Days Since Leak", "Leak*Days Since", "Days Since Decision", "Decision*Days Since"), 
         caption= dvs[i],
         caption.above = TRUE,
         custom.note = "*$p<.05$.")
}


#### two wave analyses #####

for(i in 1:length(dvs2)){
  #fe by ID
  simple_model <- 
    lm_robust(data_cross[,dvs2[i]] ~ decision_cut,
              se_type = "HC1", 
              weights= weight,
              data = data_cross)
  
  # time model 
  time_model_decision <- 
    lm_robust(data_cross[,dvs2[i]] ~ decision_cut*dayssince_decision,
              se_type = "HC1",
              data = data_cross)
  
  texreg(list(simple_model, time_model_decision), digits = 2, include.ci = TRUE, 
         file = glue("Tables/cross_{dvs2[i]}.tex"),
         stars = c(0.001, 0.01, 0.05),
         custom.model.names = c("1", "2"), 
         custom.coef.names= c('(Intercept)', "Decision", "Days Since Decision", "Decision*Days Since"), 
         caption= dvs2[i],
         caption.above = TRUE,
         custom.note = "*$p<.05$.")
}


#####################################################################
###################### exploratory analyses #########################
#####################################################################
data_panel$moral_norm

lm_robust(norm ~ decision_cut,
          se_type = "HC1", 
          fixed_effects = universal_ID,
          weights= weight_ps,
          #subset = pid == 'Democrat',
          data = data_panel)

lm_robust(norm ~ decision_cut,
          se_type = "HC1", 
          #fixed_effects = universal_ID,
          weights= weight,
          #subset = pid == 'Democrat',
          data = data_cross)
